package com.efuture.collection;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @ClassNAME TreeMapTest
 * @Description TreeMap的基本使用
 * @Author zhaoshanshan
 * @Date 2021/9/7 5:06 下午
 * @Version 1.0
 */
public class TreeMapTest {
    public static void main(String[] args) {
        //创建对象
        TreeMap treeMap = new TreeMap();
        //添加元素
        treeMap.put(1,"zhangsan");
        treeMap.put(2,"lisi");
        treeMap.put(3,"wangwu");
        treeMap.put(4,"zhaoliu");
        //取出元素
        System.out.println(treeMap.get(1));
        System.out.println(treeMap.get(2));
        System.out.println(treeMap.get(3));
        System.out.println(treeMap.get(4));
        System.out.println("===================");
        //遍历
        Set<Map.Entry<Object,Object>> treeMapTest =  treeMap.entrySet();
        Iterator it = treeMapTest.iterator();
        while (it.hasNext()){
            Map.Entry<Object,Object> node = (Map.Entry<Object,Object>)it.next();
            System.out.println(node.getKey()+"="+node.getValue());
        }
        System.out.println("===================");
        Set<Object> setTest=  treeMap.keySet();
        Iterator itSet = setTest.iterator();
        while (itSet.hasNext()){
            Object key = itSet.next();
            System.out.println(key+"="+treeMap.get(key));
        }
    }
}